package com.pur.zsms.unit.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.CipherUtil;
import com.pur.zsms.foodmanage.food.dao.IFoodDao;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.service.IFoodService;
import com.pur.zsms.foodmanage.food.vo.FoodMaterVo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.po.RolePo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.UnitMenuPo;
import com.pur.zsms.pmsnmanage.user.po.UnitPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.unit.dao.IUnitDao;
import com.pur.zsms.unit.service.IUnitService;
import com.pur.zsms.unit.vo.UnitCommentVo;
import com.pur.zsms.unit.vo.UnitMenuVo;
import com.pur.zsms.unit.vo.UnitVo;

@Service("unitService")
public class UnitServiceImpl implements IUnitService {

	@Resource(name = "unitDao")
	private IUnitDao unitDaoImpl;

	public IUnitDao getUnitDaoImpl() {
		return unitDaoImpl;
	}

	public void setUnitDaoImpl(IUnitDao unitDaoImpl) {
		this.unitDaoImpl = unitDaoImpl;
	}

	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public PaginationSupport listUnitComment(PageController pageController, UnitCommentVo unitCommentVo)
	        throws GenericException {
		return unitDaoImpl.listUnitComment(pageController, unitCommentVo);
	}

	public PaginationSupport listUnit(PageController pageController, UnitVo unitVo) throws GenericException {
		return unitDaoImpl.listUnit(pageController, unitVo);

	}

	public PaginationSupport listUnitMenu(PageController pageController, UnitMenuVo unitMenuVo)
	        throws GenericException {
		return unitDaoImpl.listUnitMenu(pageController, unitMenuVo);
	}

	public String getAllUnitPoJsonString(int space) throws GenericException {
		List<UnitPo> tUnitPoList = null;
		tUnitPoList = unitDaoImpl.getAllUnitPo();

		StringBuffer ql = new StringBuffer();
		ql.append("[");
		if (space == 1) {
			ql.append("{\"unitId\":\"0\",\"unitName\":\"无\"},");
		}
		String q2 = "";
		if (tUnitPoList != null && tUnitPoList.size() > 0) {
			for (UnitPo unitPo : tUnitPoList) {
				ql.append("{\"unitId\":\"" + unitPo.getUnitId() + "\",\"unitName\":\"" + unitPo.getUnitName()
				        + "\"},");
			}
			q2 = ql.substring(0, ql.length() - 1) + "]";
		}
		return q2;
	}

	public boolean addUnitMenu(UnitMenuVo c) throws GenericException {
		// 首先校验状态

		String unitCode = unitDaoImpl.getUnitPoByUnitId(c.getUnitId());
		c.setUnitCode(unitCode);
		List<UnitMenuPo> list = unitDaoImpl.getUnitMenuPo(c);
		if (list != null && list.size() > 0) {
			msg = "msg001";
			return false;
		}
		//
		BeanCopier copier = BeanCopier.create(UnitMenuVo.class, UnitMenuPo.class, false);
		UnitMenuPo unitMenuPo = new UnitMenuPo();
		copier.copy(c, unitMenuPo, null);
		unitDaoImpl.addUnitMenuPo(unitMenuPo);

		return true;
	}

	public boolean updateUnitMenu(UnitMenuVo c) throws GenericException {

		BeanCopier copier = BeanCopier.create(UnitMenuVo.class, UnitMenuPo.class, false);
		UnitMenuPo unitMenuPo = new UnitMenuPo();
		copier.copy(c, unitMenuPo, null);
		unitDaoImpl.addUnitMenuPo(unitMenuPo);

		return true;
	}

	public UnitMenuVo getUnitMenuPo(UnitMenuVo c) throws GenericException {
		UnitMenuPo tUnitMenuPo = unitDaoImpl.getUnitMenuPoByVo(c).get(0);

		int unitId = unitDaoImpl.getUnitIdByUnitCode(tUnitMenuPo.getUnitCode());
		
		UnitMenuVo tUnitMenuVo = new UnitMenuVo();
		BeanCopier copier = BeanCopier.create(UnitMenuPo.class, UnitMenuVo.class, false);
		copier.copy(tUnitMenuPo, tUnitMenuVo, null);
		tUnitMenuVo.setUnitId(unitId);

		return tUnitMenuVo;
	}

}
